#include "StdAfx.h"
#include "DataServer.h"
//#include "ProcessInterfaceTcpShort.h"
#include "ProtocolConnectPoint.h"
#include "ProcessInterfaceA4C.h"
#include "ConfigImpl.h"
#include "CommHandler.h"

DataServer::DataServer(void)
{
	m_pCP = NULL;
	m_pCpMgr = NULL;
	//m_nUnSendTimout = 1;
	//m_nUnBlockTimout = 1;
}

int DataServer::g_socketID = 1;
HEADER_REQ DataServer::g_head;

DataServer::~DataServer(void)
{
}

int DataServer::Init( CConfig* pConfig )
{
	//CConfigImpl config;
	//IP_PORT stIpPort;
	//stIpPort.sIp = "127.0.0.1";
	//stIpPort.nPort = 26000;
	//config.SetProperty("ip_port", "127.0.0.1:26000");
	m_pCP = new CProtocolCpCli<CProcessInterfaceA4C>();
	LOG_POINT(m_pCP);
	m_pCP->Init(pConfig);
	//m_pCP->SetIp(stIpPort.sIp, stIpPort.nPort);
	
	g_head.SetMsgFlag("1");
	g_head.SetMsgType("1");
	g_head.SetSockID((int)m_pCP);
	return 0;
}

int DataServer::SendPacket( CPacket &pkt )
{
	try
	{
		//CRLog(E_DEBUG,"CMainServiceHandler::SendPacket CTradePacket %s ", pkt.GetCmdID().c_str());
		CTradePacket& pktTrade = dynamic_cast<CTradePacket&>(pkt);		
		
		g_head.SetSeqNo(g_socketID++);
		pktTrade.SetHeader(g_head);
		TRACE(pktTrade.GetCmdID().c_str());
		CGessGuard g(m_mutextCp);

		int nRet = 0;
		if (m_pCP)
		{
			CTradePacket rsp;
			DWORD dwT1 = GetTickCount();
			//nRet = m_pCP->SendPacket(pktTrade, rsp, m_nUnSendTimout, m_nUnBlockTimout);
			nRet = m_pCP->SendPacket(pktTrade);
			//CRLog(E_DEBUG, "request %s tick=%d, nRet=%d",pktTrade.GetCmdID().c_str(), GetTickCount()- dwT1,nRet);
			//if (0 == nRet)
			//{
			//	return RunPacketHandleApi(rsp, it->first);
			//}
			return nRet;
		}

		return -2;
	}
	catch(std::bad_cast)
	{
	}
	catch(std::exception e)
	{
		CRLog(E_ERROR,"exception:%s!",e.what());
		return -1;
	}
	catch(...)
	{
		CRLog(E_ERROR,"%s","Unknown exception!");
		return -1;
	}
	return -3;
}

int DataServer::SendPacket( CTradePacket &req, CTradePacket &rsp )
{
	try
	{
		//CRLog(E_DEBUG,"CMainServiceHandler::SendPacket CTradePacket %s ", pkt.GetCmdID().c_str());
		//CBtsPacket& pktTrade = dynamic_cast<CBtsPacket&>(pkt);		
		//TRACE(pktTrade.GetCmdID().c_str());
		CGessGuard g(m_mutextCp);

		g_head.SetSeqNo(g_socketID++);
		int nRet = 0;
		if (m_pCP)
		{
			//CTradePacket rspBase;
			//nRet = m_pCP->SendPacket(req, rsp, m_nUnSendTimout, m_nUnBlockTimout);
			nRet = m_pCP->SendPacket(req);
			if (0 == nRet)
			{
				//rsp.Decode(rspBase);
				return 0;
			}
			else
			{
				//CRLog(E_ERROR,"%x SendPacket fail %d", req.GetWordCmdID(),nRet);
			}
		}

		return -2;
	}
	catch(std::bad_cast)
	{
	}
	catch(std::exception e)
	{
		CRLog(E_ERROR,"exception:%s!",e.what());
		return -1;
	}
	catch(...)
	{
		CRLog(E_ERROR,"%s","Unknown exception!");
		return -1;
	}
	return -3;
}

void DataServer::Bind( CConnectPointManager* pCpMgr,const unsigned long& ulKey )
{
	m_ulKey = ulKey; 
	m_pCpMgr = dynamic_cast<CCommHandler*>(pCpMgr);
	if (m_pCP)
		m_pCP->Bind(m_pCpMgr, ulKey);
}

void DataServer::Stop()
{
	if (m_pCP)
		m_pCP->Stop();
}

void DataServer::Finish()
{
	if (m_pCP)
		m_pCP->Finish();
}

int DataServer::Start()
{
	if (m_pCP)
		m_pCP->Start();
	return 0;
}

//*********************************ServerF5******************************
int ServerF5::g_socketID = 2;
HEADER_REQ ServerF5::g_head;
void ServerF5::Bind( CConnectPointManager* pCpMgr,const unsigned long& ulKey )
{
	m_ulKey = ulKey; 
	m_pCpMgr = dynamic_cast<CCommHandler*>(pCpMgr);
	if (m_pCP)
		m_pCP->Bind(m_pCpMgr, ulKey);
}

int ServerF5::Init( CConfig* pConfig )
{
	m_pCP = new CProtocolCpCli<CProcessInterfaceA4C>();
	LOG_POINT(m_pCP);
	m_pCP->Init(pConfig);
	//m_pCP->SetIp(stIpPort.sIp, stIpPort.nPort);

	g_head.SetMsgFlag("1");
	g_head.SetMsgType("1");
	g_head.SetSockID((int)m_pCP);
	return 0;
}

int ServerF5::SendPacket( CPacket &pkt )
{
	try
	{
		//CRLog(E_DEBUG,"CMainServiceHandler::SendPacket CTradePacket %s ", pkt.GetCmdID().c_str());
		CTradePacket& pktTrade = dynamic_cast<CTradePacket&>(pkt);		

		g_head.SetSeqNo(CTradePacket::GetSID());
		pktTrade.SetHeader(g_head);
		TRACE(pktTrade.GetCmdID().c_str());
		CGessGuard g(m_mutextCp);

		int nRet = 0;
		if (m_pCP)
		{
			CTradePacket rsp;
			DWORD dwT1 = GetTickCount();
			//nRet = m_pCP->SendPacket(pktTrade, rsp, m_nUnSendTimout, m_nUnBlockTimout);
			nRet = m_pCP->SendPacket(pktTrade);
			//CRLog(E_DEBUG, "request %s tick=%d, nRet=%d",pktTrade.GetCmdID().c_str(), GetTickCount()- dwT1,nRet);
			//if (0 == nRet)
			//{
			//	return RunPacketHandleApi(rsp, it->first);
			//}
			return nRet;
		}

		return -2;
	}
	catch(std::bad_cast)
	{
	}
	catch(std::exception e)
	{
		CRLog(E_ERROR,"exception:%s!",e.what());
		return -1;
	}
	catch(...)
	{
		CRLog(E_ERROR,"%s","Unknown exception!");
		return -1;
	}
	return -3;
}
